package com.xingxuecn.demos.service.impl;

import com.xingxuecn.demos.dao.GoodsDao;
import com.xingxuecn.demos.service.UserService;

import org.apache.commons.lang3.builder.MultilineRecursiveToStringStyle;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * Autowired注解自动注入，先按照属性名去匹配，其次按照类型匹配
 * Resource注解注入，先按照属性名去匹配，其次按照类型匹配，如果配置name、type参数，会覆盖配置规则
 * Required注解用于属性setter方法，表示这个属性必须传值初始化
 *
 * @author Kris at 2018/9/26 10:21
 */
@Service("goodsService")
public class GoodsService {
/*
* Autowired 和 Resource的区别
* 1.Autowired是Spring提供的注解，Resource Java（javax）提供的注解
* 2.Autowired和Resource都会先根据属性名匹配，其次根据类型匹配。但是Resource可以通过name、type参数改变这个规则依据
* 3.Autowired要指明name，需配合Qualifier注解来使用，Resource可以直接在参数指定
*
* */


    @Autowired(required = true)
    //@Qualifier("goodsDao")//通过Qualifier指明需要注入的bean的名称
    private GoodsDao goodsDao;

    @Resource
    private UserService userService1;



    @PostConstruct
    public void init(){
        System.out.println("Bean is going through init.");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("Bean will destroy now.");
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, new MultilineRecursiveToStringStyle())
                .append("goodsDao", goodsDao)
                .append("userService", userService1)
                .toString();
    }
}
